package com.itbaizhan.clouddemoproduct.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.clouddemoproduct.domain.Product;
import com.itbaizhan.clouddemoproduct.dto.ProductDto;
import com.itbaizhan.clouddemoproduct.mapper.ProductMapper;
import com.itbaizhan.clouddemoproduct.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    /**
     * 查询商品
     * @param model
     * @return
     */
    @GetMapping("/index")
    public ModelAndView selectProduct(Model model){
        ProductDto productDto = new ProductDto();
        //获取认证对象, 从中拿到token
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        Page<Product> productPage = productService.selectProduct(productDto);
        ModelAndView modelAndView = new ModelAndView("product"); // 跳转到 product.html
        modelAndView.addObject("productList", productPage.getRecords());
        modelAndView.addObject("token", details.getTokenValue());   //获取token值,放入token对象传给前端
        modelAndView.addObject("total", productPage.getTotal());
        return modelAndView;
    }


}
